import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import { getTest, getList } from '../api/api'


export const sourceSlice = createSlice({
  name: 'source',
  initialState: {
    test: '测试 - 测试',
    list: []
  },
  reducers: { // 同步修改
    clsList(state, { payload }) {
      state.list = payload
    }
  },
  extraReducers: (builder) => { // 异步修改
    builder
      .addCase(getTestAsync.fulfilled, (state, action) => {
        state.test = action.payload;
      })
      .addCase(getListAsync.fulfilled, (state, action) => {
        state.list = action.payload;
      })
  }
})


export const { clsList } = sourceSlice.actions // 同步修改方法导出

export const getTestAsync = createAsyncThunk('source/test', async () => {
  const res = await getTest()
  return res.data.msg
})

export const getListAsync = createAsyncThunk('source/list', async () => {
  const res = await getList()
  return res.data.result
})

export default sourceSlice.reducer // 默认导出
